package com.qf.service.impl;

import com.qf.dao.repository.XcCityRepository;
import com.qf.pojo.po.XcCity;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.XcCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class XcCityServiceImpl implements XcCityService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    XcCityRepository xcCityRepository;
   /* @Autowired
    XcCityAreaRepository xcCityAreaRepository;*/
    @Override
    public BaseResp findAll() {
        List<XcCity> list=new ArrayList<>();
        Boolean city_list = redisTemplate.hasKey("CITY_LIST");
        if (!city_list){
            List<XcCity> all = xcCityRepository.findAll();
            HashSet<Object> objects = new HashSet<>();
            for (XcCity xcCity : all) {
                objects.add(xcCity);
            }
            redisTemplate.opsForList().leftPushAll("CITY_LIST",objects);
            list=redisTemplate.opsForList().range("CITY_LIST",0,-1);
        }else {
            list = redisTemplate.opsForList().range("CITY_LIST", 0, -1);
        }
        return new BaseResp(0,"查询成功",null,list);
    }

  /*  @Override
    public BaseResp findAll() {
         List<XcCityArea> list=new ArrayList<>();
        Boolean area_list = redisTemplate.hasKey("AREA_LIST");
        if (!area_list){
            List<XcCityArea> all=xcCityAreaRepository.findAll();
             redisTemplate.opsForList().leftPushAll("AREA_LIST", all);
            list = redisTemplate.opsForList().range("AREA_LIST", 0, -1);
        }else {
            list = redisTemplate.opsForList().range("AREA_LIST", 0, -1);
        }
        return new BaseResp(0,"查询成功",null,list);
    }*/
}
